<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\PigEat;
use think\facade\Db;

class PigEatLogic
{
    protected PigEat $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new PigEat();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @author: zj
     * @param mixed $Id
     * @param array $param
     * @time 2024/03/14 15:34:22
     * @return true
     */
    public function addEdit(mixed $Id, array $param)
    {
        Db::startTrans();
        try {
            $exist = $this->model->checkOutIdIsExist($this->factory_id,$Id);
            if ($exist) {
                $rule = [
                    'PigId' => 'integer|>:0',
                    'FeedName' => 'max:255',
                    'FeedWeight' => 'integer|>:0',
                    'MedicineInfo' => 'max:1500',
                    'WaterRate' => 'integer|>=:0',
                    'AllWeight' => 'integer|>:0',
                    'EatingTime' => 'integer|>:0',
                    'PreFeedDayId' => 'integer|>:0',
                ];
                validate($rule)->check($param);
                
                $updateDate = [];
                if ($param['PigId']) {
                    $updateDate['out_pig_id'] = $param['PigId'];
                }
                if ($param['FeedName']) {
                    $updateDate['feed_name'] = $param['FeedName'];
                }
                if ($param['FeedWeight']) {
                    $updateDate['feed_weight'] = $param['FeedWeight'];
                }
                if (isset($param['MedicineInfo'])) {
                    $updateDate['medicine_info'] = $param['MedicineInfo'];
                }
                if (isset($param['WaterRate'])) {
                    $updateDate['water_rate'] = $param['WaterRate'];
                }
                if ($param['AllWeight']) {
                    $updateDate['all_weight'] = $param['AllWeight'];
                }
                if ($param['EatingTime']) {
                    $updateDate['eating_time'] = $param['EatingTime'];
                }
                if ($param['PreFeedDayId']) {
                    $updateDate['feed_plan_id'] = $param['PreFeedDayId'];
                }
                $updateDate['update_record_time'] = $param['RecordTime'];
                $updateDate['update_time'] = request()->time();

                $this->model->where('out_id',$Id)->update($updateDate);
            } else {
                $rule = [
                    'PigId' => 'require|integer|>:0',
                    'FeedName' => 'require|max:255',
                    'FeedWeight' => 'require|integer|>:0',
                    'MedicineInfo' => 'require|max:1500',
                    'WaterRate' => 'require|integer|>=:0',
                    'AllWeight' => 'require|integer|>:0',
                    'EatingTime' => 'require|integer|>:0',
                    'PreFeedDayId' => 'require|integer|>:0',
                ];
                validate($rule)->check($param);
                
                $this->model->insert([
                    'out_id' => $Id,
                    'factory_id' => $this->factory_id,
                    'out_pig_id' => $param['PigId'],
                    'feed_name' => $param['FeedName'],
                    'feed_weight' => $param['FeedWeight'],
                    'medicine_info' => $param['MedicineInfo'],
                    'water_rate' => $param['WaterRate'],
                    'all_weight' => $param['AllWeight'],
                    'eating_time' => $param['EatingTime'],
                    'feed_plan_id' => $param['PreFeedDayId'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);
            }

            Db::commit();
        }catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }
}